const Joi = require('joi')
const R = require('@/models/Response')
const { ERROR_USER_CODE, ERROR_SYSTEM_CODE } = require('@/constants/errorCode')
const { IS_INTEGER, IS_UNSAFE_INTEGER } = require('@/constants/joiSchema')
const { dumpCheck: globalDumpCheck } = require('@/utils/dumpCheck')

const questionSchema = {
  questionIds: Joi.array().items(IS_UNSAFE_INTEGER),
  questionId: IS_UNSAFE_INTEGER,
  page: IS_INTEGER.min(1).default(1),
  pageSize: IS_INTEGER.min(1).default(999),
  name: Joi.string().min(1).max(50),
  pic: Joi.string().allow(''),
  answer: Joi.string(),
  type: IS_INTEGER.valid(1, 2, 3),
  score: IS_INTEGER,
  paperId: IS_UNSAFE_INTEGER,
  options: Joi.string()
}

/**
 * @description 问题列表查询的参数验证
 * @param {*} ctx
 * @param {*} next
 * @returns
 */
module.exports.listQuestionValidate = async (ctx, next) => {
  const schema = Joi.object({
    page: questionSchema.page,
    pageSize: questionSchema.pageSize,
    name: questionSchema.name,
    paperId: questionSchema.paperId,
    type: questionSchema.type
  }).validate(ctx.query)

  if (schema.error) {
    ctx.body = R.error(ERROR_SYSTEM_CODE.PARAMS_SCHEMA_ERROR)
    return
  }
  ctx.newQuery = schema.value
  await next()
}

/**
 * @description 创建问题时的参数验证
 * @param {*} ctx
 * @param {*} next
 * @returns
 */
module.exports.createQuestionValidate = async (ctx, next) => {
  const schema = Joi.object({
    name: questionSchema.name.required(),
    paperId: questionSchema.paperId.required(),
    pic: questionSchema.pic,
    analysis: questionSchema.pic.required(),
    anaPics: questionSchema.pic,
    answer: questionSchema.answer.required(),
    options: questionSchema.options,
    type: questionSchema.type.required(),
    score: questionSchema.score
  }).validate(ctx.request.body)

  if (schema.error) {
    ctx.body = R.error(ERROR_SYSTEM_CODE.PARAMS_SCHEMA_ERROR)
    return
  }

  const { paperId } = ctx.request.body

  const b4 = await globalDumpCheck.checkPaperIds([paperId], true)
  // TODO 验证pid的合法性
  if (!b4) {
    ctx.body = R.error(ERROR_USER_CODE.PAPER_ID_ERROR)
    return
  }

  await next()
}

/**
 * @author: Hejh(3010733382@qq.com)
 * @description: 更新问题时的参数验证
 * @param {*} ctx
 * @param {*} next
 */
module.exports.updateQuestionValidate = async (ctx, next) => {
  const schema = Joi.object({
    questionId: questionSchema.questionId.required(),
    name: questionSchema.name.required(),
    paperId: questionSchema.paperId.required(),
    pic: questionSchema.pic,
    answer: questionSchema.answer.required(),
    options: questionSchema.options,
    type: questionSchema.type.required(),
    score: questionSchema.score.required()
  }).validate(ctx.request.body)

  if (schema.error) {
    ctx.body = R.error(ERROR_SYSTEM_CODE.PARAMS_SCHEMA_ERROR)
    return
  }

  //TODO 验证 pid 和 questionId的合法性
  const { questionId, paperId } = ctx.request.body
  const b4 = await globalDumpCheck.checkQuestionIds([questionId])
  const b5 = await globalDumpCheck.checkPaperIds([paperId])
  if (!b4 || !b5) {
    ctx.body = R.error(ERROR_USER_CODE.EXAM_ID_ERROR)
    return
  }

  await next()
}

/**
 * @author: Hejh(3010733382@qq.com)
 * @description: 批量删除问题接口的参数验证
 * @param {*} ctx
 * @param {*} next
 */
module.exports.delQuestionsValidate = async (ctx, next) => {
  const schema = Joi.object({
    questionIds: questionSchema.questionIds.required()
  }).validate(ctx.request.body)

  if (schema.error) {
    ctx.body = R.error(ERROR_SYSTEM_CODE.PARAMS_SCHEMA_ERROR)
    return
  }

  const { questionIds } = ctx.request.body

  // 验证问题ID数组的合法性
  const b4 = await globalDumpCheck.checkQuestionIds(questionIds)
  if (!b4) {
    ctx.body = R.error(ERROR_USER_CODE.QUESTION_ID_ERROR)
    return
  }
  await next()
}
